<template>
  <el-card class='main-card'>
    <div class='title'>{{ this.$route.name }}</div>
    <div class='book-status-menu'>
      <span>状态</span>
      <span @click="changeStatus('all')" :class="isActive('all')">全部</span>
      <span @click="changeStatus('delete')" :class="isActive('delete')"> 回收站 </span>
    </div>
    <div class='operation-container'>
      <el-button
        v-if='isDelete === 0'
        type='danger'
        size='small'
        icon='el-icon-delete'
        :disabled='bookIds.length === 0'
        @click='updateIsDelete = true'>
        批量删除
      </el-button>
      <el-button
        v-else
        type='danger'
        size='small'
        icon='el-icon-delete'
        :disabled='bookIds.length === 0'
        @click='remove = true'>
        批量删除
      </el-button>
      <div style='margin-left: auto'>
        <el-input
          clearable
          v-model='keywords'
          prefix-icon='el-icon-search'
          size='small'
          placeholder='请输入书名'
          style='width: 200px;'
          @change='searchBooks' />
        <el-button type='primary' size='small' icon='el-icon-search' style='margin-left: 1rem' @click='searchBooks'>
          搜索
        </el-button>
      </div>
    </div>
    <el-table
      :data='books'
      border
      style='width: 100%'
      @selection-change='handleSelectionChange'>
      <el-table-column
        type='selection'
        width='50'
        align='center'>
      </el-table-column>
      <el-table-column
        fixed
        prop='id'
        label='ID'
        width='50'
        align='center'>
      </el-table-column>
      <el-table-column
        label='书籍封面'
        align='center'
        width='180'
        v-loading='loading'>
        <template slot-scope='scope'>
          <el-image
            :src='scope.row.bookCover'
            fit='cover'
          />
          <i class='el-icon-collection article-status-icon' />
        </template>
      </el-table-column>
      <el-table-column
        prop='bookName'
        label='书名'
        width='180'
        align='center'
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop='bookAuthor'
        label='作者'
        width='120'
        align='center'
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column label='标签' width='140' align='center'>
        <template slot-scope='scope'>
          <el-tag v-for="item of scope.row.bookTags" :key='item.bookId' style="margin-right: 0.2rem; margin-top: 0.2rem">
            {{ item.tagName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop='bookProfile' label='书籍简介' width='300' align='center'></el-table-column>
      <el-table-column prop='bookUrl' label='地址链接' width='240' align='center'></el-table-column>
      <el-table-column prop='bookRate' label='推荐指数' align='center' width='180'>
        <template slot-scope='scope'>
          <el-rate
            v-model="scope.row.bookRate"
            show-score
            :colors="colors"
          @change='changeIsRecommend(scope.row)'>
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column prop='isRecommend' label='是否推荐' width='80' align='center'>
        <template slot-scope='scope'>
          <el-switch
            v-model='scope.row.isRecommend'
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :disabled="scope.row.isDelete === 1"
            :active-value="1"
            :inactive-value="0"
            @change='changeIsRecommend(scope.row)'>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed='right' label='操作' width='150' align='center'>
        <template slot-scope='scope'>
          <el-button @click='editBook(scope.row.id)' size='small' type='primary' plain v-if='scope.row.isDelete === 0'>编辑</el-button>
          <el-popconfirm
            title="确定删除吗？"
            style="margin-left: 10px"
            @confirm="updateBookDelete(scope.row.id)"
            v-if="scope.row.isDelete === 0">
            <el-button size="small" type="danger" plain slot="reference"> 删除 </el-button>
          </el-popconfirm>
          <el-button @click='updateBookDelete(scope.row.id)' size='small' type='success' plain v-if='scope.row.isDelete === 1'> 恢复</el-button>
          <el-popconfirm
            style="margin-left: 10px"
            v-if="scope.row.isDelete === 1"
            title="确定彻底删除吗？"
            @confirm="deleteBooks(scope.row.id)">
            <el-button size="small" type="danger" plain slot="reference"> 删除 </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class='pagination-container'
      background
      @size-change='sizeChange'
      @current-change='currentChange'
      :current-page='current'
      :page-size='size'
      :total='count'
      :page-sizes='[5, 10, 20]'
      layout='total, sizes, prev, pager, next, jumper' />
    <!--  删除提示框  -->
    <el-dialog :visible.sync='updateIsDelete' width='30%'>
      <div class='dialog-title-container' slot='title'><i class='el-icon-warning' style='color: #ff9900' />提示</div>
      <div style='font-size: 1rem'>是否删除选中项？</div>
      <div slot='footer'>
        <el-button @click='updateIsDelete = false'>取 消</el-button>
        <el-button type='primary' @click='updateBookDelete(null)'> 确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  name: 'list.vue',
  created() {
    this.listBooks()
  },
  data() {
    return {
      uploadHeaders: { Authorization: 'Bearer ' + sessionStorage.getItem('token') },
      loading: true,
      books: [],
      updateIsDelete: false,
      isDelete: 0,
      bookIds: [],
      keywords: null,
      current: 1,
      size: 10,
      count: 0,
      bookId: null,
      remove: false,
      status: null,
      activeStatus: 'all',
      colors: ['#99A9BF', '#F7BA2A', '#FF9900']
    }
  },
  methods: {
    editBook(id) {
      console.log(id)
      this.$router.push({ path: '/book/' + id })
    },
    changeStatus(status) {
      switch (status) {
        case 'all':
          this.isDelete = 0
          break
        case 'delete':
          this.isDelete = 1
          break
      }
      this.current = 1
      this.activeStatus = status
    },
    // 逻辑删除或者恢复操作
    updateBookDelete(id) {
      let param = {}
      if (id != null) {
        param.ids = [id]
      } else {
        param.ids = this.bookIds
      }
      param.isDelete = (this.isDelete === 0 ? 1 : 0)
      this.axios
        .put('/api/admin/book/delete', param)
        .then(({ data }) => {
          if (data.flag) {
            this.$notify.success({
              title: '成功',
              message: data.message
            })
            this.listBooks()
          } else {
            this.$notify.error({
              title: '失败',
              message: data.message
            })
          }
          this.updateIsDelete = false
        })
    },
    // 物理删除
    deleteBooks(id){
      let param = {}
      if (id == null) {
        param = { data: this.bookIds }
      } else {
        param = { data: [id] }
      }
      this.axios.delete('/api/admin/book/delete', param).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: data.message
          })
          this.listBooks()
        } else {
          this.$notify.error({
            title: '失败',
            message: data.message
          })
        }
        this.remove = false
      })
    },
    //书籍推荐
    changeIsRecommend(book){
      this.axios
        .put('/api/admin/book/recommend',{
          id:book.id,
          bookRate: book.bookRate,
          isRecommend: book.isRecommend
        }).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: '修改成功'
          })
        } else {
          this.$notify.error({
            title: '失败',
            message: data.message
          })
        }
        this.remove = false
      })
    },
    sizeChange(size) {
      this.size = size
      this.listBooks()
    },
    currentChange(current) {
      this.current = current
      this.$store.commit('updateBookListPageState', current)
      this.listBooks()
    },
    handleSelectionChange(books) {
      this.bookIds = []
      books.forEach((item) => {
        this.bookIds.push(item.id)
      })
    },
    searchBooks() {
      this.current = 1
      this.listBooks()
    },
    listBooks() {
      this.axios
        .get('/api/admin/book/list', {
          params: {
            current: this.current,
            size: this.size,
            keywords: this.keywords,
            bookId: this.bookId,
            isDelete: this.isDelete
          }
        }).then(({ data }) => {
        this.books = data.data.records
        this.count = data.data.count
        this.loading = false
      })
    }
  },
  watch: {
    type() {
      this.current = 1
      this.listBooks()
    },
    isDelete() {
      this.current = 1
      this.listBooks()
    }
  },
  computed: {
    isActive() {
      return function(status) {
        return this.activeStatus === status ? 'active-status' : 'status'
      }
    }
  }
}
</script>

<style scoped>
.book-status-menu {
    font-size: 14px;
    margin-top: 40px;
    color: #999;
}

.book-status-menu span {
    margin-right: 24px;
}

.article-status-icon {
    color: #fff;
    font-size: 1.5rem;
    position: absolute;
    right: 1rem;
    bottom: 1.4rem;
}

.status {
    cursor: pointer;
}

.active-status {
    cursor: pointer;
    color: #333;
    font-weight: bold;
}
</style>
